//
//  QualityAlert.swift
//  33web
//
//  Created by 宋海胜 on 2021/6/9.
//

import UIKit
import SnapKit

class QualityAlert: UIView, UITableViewDelegate, UITableViewDataSource {
    
    public var clickedClosure: ((Int) -> Void)?
    
    var selectIndex: Int = 0
    
    var tableView: UITableView!

    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor.white
        v.layer.cornerRadius = 5
        return v
    }()
    
    let back: UIButton = {
        let v: UIButton  = UIButton()
        v.backgroundColor = UIColor.clear
        return v
    }()
    
    let titleLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = UIFont.systemFont(ofSize: 17)
        v.text = "鸡蛋状态"
        v.textAlignment = .center
        return v
    }()
    
    let cancel: UIButton = {
        let v: UIButton  = UIButton()
        v.setTitle("取消", for: .normal)
        v.setTitleColor(UIColor.white, for: .normal)
        v.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        v.backgroundColor = .blue
        v.layer.cornerRadius = 3
        return v
    }()
    
    let sure: UIButton = {
        let v: UIButton  = UIButton()
        v.setTitle("提交", for: .normal)
        v.setTitleColor(UIColor.white, for: .normal)
        v.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        v.backgroundColor = .blue
        v.layer.cornerRadius = 3
        return v
    }()
    
    public var lineView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor(hex: 0xE6E6E6)
        return v
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initViews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    func initViews() {
        
        sure.addTarget(self, action: #selector(sureAction), for: .touchUpInside)
        cancel.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        back.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        self.backgroundColor = UIColor(white: 0, alpha: 0.5)
        addSubview(back)
        addSubview(bgView)
        bgView.addSubview(titleLb)
        bgView.addSubview(sure)
        bgView.addSubview(cancel)
        bgView.addSubview(lineView)
        bgView.addSubview(lineView)
        back.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        bgView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(280.0)
            make.height.equalTo(200.0)
        }

        titleLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(40.0)
        }

        sure.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-15)
            make.right.equalToSuperview().offset(-25)
            make.height.equalTo(30)
            make.width.equalTo(100)
        }
        cancel.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-15)
            make.left.equalToSuperview().offset(25)
            make.height.equalTo(30)
            make.width.equalTo(100)
        }
        lineView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(40)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        setTableView()
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect.zero, style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.isScrollEnabled = false
        self.tableView!.register(QualityAlertCell.self, forCellReuseIdentifier: "Cell")
        self.bgView.addSubview(self.tableView!)
        tableView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(40)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(90)
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: QualityAlertCell? = tableView.dequeueReusableCell(withIdentifier: "Cell") as? QualityAlertCell
        cell?.selectionStyle = .none
        if indexPath.row == 0 {
            cell?.nameLb.text  =  "有蛋"
        } else if indexPath.row == 1 {
            cell?.nameLb.text  =  "没有蛋"
        } else  {
            cell?.nameLb.text  =  "坏蛋"
        }
        if indexPath.row == selectIndex {
            cell?.img.image = UIImage(named: "ticket_choice_select")
        } else {
            cell?.img.image = UIImage(named: "ticket_choice")
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectIndex = indexPath.row
        self.tableView.reloadData()
    }
    
    @objc func backAction() {
        self.isHidden = true
    }
    @objc func sureAction() {
        self.isHidden = true
        self.clickedClosure?(selectIndex)
    }
    
}

class QualityAlertCell: UITableViewCell  {
    
    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    
    var img: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "ticket_choice")
        return v
    }()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initViews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    func initViews() {
        
        contentView.addSubview(nameLb)
        contentView.addSubview(img)
        
        nameLb.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(50)
            make.top.bottom.equalToSuperview()
            make.height.equalTo(30)
        }
        img.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().offset(3)
            make.left.equalToSuperview().offset(25)
            make.height.equalTo(20)
            make.width.equalTo(20)
        }
    }
}




extension UIColor {
    /**
         Make color with hex string
         - parameter hex: 16进制字符串(eg. #0x00eeee or #0X00eeee or 0x00eeee or 0X00eeee or 00eeee)
         - returns: RGB
         */
        static func hexString(hex: String, alpha: CGFloat = 1.0) -> UIColor {
            
            var cString: NSString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() as NSString
            
            if (cString.hasPrefix("#")) {
                cString = cString.substring(from: 1) as NSString
            }
            if (cString.hasPrefix("0X") || cString.hasPrefix("0x")) {
                cString = cString.substring(from: 2) as NSString
            }
            
            if (cString.length != 6) {
                return UIColor.gray
            }
            
            let rString = cString.substring(with: NSMakeRange(0, 2))
            let gString = cString.substring(with: NSMakeRange(2, 2))
            let bString = cString.substring(with: NSMakeRange(4, 2))
            
            var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0
            
            Scanner(string: rString).scanHexInt32(&r)
            Scanner(string: gString).scanHexInt32(&g)
            Scanner(string: bString).scanHexInt32(&b)
            
            return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: alpha)
        }
    
    /// 传入16进制
    convenience init(hex: Int, alpha: CGFloat = 1.0) {
        self.init(red: ((CGFloat)((hex & 0xFF0000) >> 16)) / 255.0,
        green: ((CGFloat)((hex & 0xFF00) >> 8)) / 255.0,
        blue: ((CGFloat)(hex & 0xFF)) / 255.0,
        alpha: alpha)
    }
}
